import ViewUiPlus from 'view-ui-plus'
import {getToken} from '@/utils/auth'
import {NavigationGuard,NavigationHookAfter} from 'vue-router'
import {defaultDocumentTitle,noCheckList} from '@/global.config'

export const beforeEach:NavigationGuard = (to,from)=>{
    ViewUiPlus.LoadingBar.start()
    // 检查token
    const hasToken = getToken()
    // 设置标题
    document.title = (to.meta.title ? to.meta.title + ' - ': '') +  defaultDocumentTitle

    if(hasToken){
        if(to.path === '/login'){
            return true
        }else{
            return true
            // 检查是否有权限 并路由已经添加
            return {
                path: '/login',
                query:{
                    redirect:to.path
                }
            }
        }
    }else{
        if(noCheckList.indexOf(to.path) !== -1){
            return true
        }else{
            return {
                path: '/login',
                query:{
                    redirect:to.path
                }
            }
        }
    }
}

export const afterEach:NavigationHookAfter = (to,from)=>{
    ViewUiPlus.LoadingBar.finish()
}
